from rest_framework import serializers

from field.models import *
from system.models import Address


# 资源表
class Resource(models.Model):
    # 资源编号，如20250824001910
    year = models.IntegerField()  # 年度
    address = models.ForeignKey(Address, on_delete=models.PROTECT, null=True, blank=True)  # 地址，外键关联Address表
    vegetation_ecological_zone = models.ForeignKey(VegetationEcologicalZone, on_delete=models.PROTECT, null=True,
                                                   blank=True)  # 植被生态区，外键关联
    ecological_engineering_zone = models.ForeignKey(EcologicalEngineeringZone, on_delete=models.PROTECT, null=True,
                                                    blank=True)  # 生态工程区，外键关联
    forest_class = models.ForeignKey(ForestClass, on_delete=models.PROTECT, null=True, blank=True)  # 森林类别，外键关联
    ecological_zone = models.ForeignKey(EcologicalZone, on_delete=models.PROTECT, null=True, blank=True)  # 生态分区，外键关联
    ecological_zone_2 = models.CharField(max_length=100, blank=True, null=True)  # 生态分区2
    dominant_tree_species = models.ForeignKey(TreeSpecies, on_delete=models.PROTECT, null=True, blank=True)  # 优势树种，外键关联
    origin = models.ForeignKey(Origin, on_delete=models.PROTECT, null=True, blank=True)  # 起源，外键关联
    age_group = models.ForeignKey(AgeGroup, on_delete=models.PROTECT, null=True, blank=True)  # 林龄组，外键关联

    area = models.FloatField(blank=True, null=True)  # 面积（公顷）
    stock = models.FloatField(blank=True, null=True)  # 立木蓄积（立方米）
    stock_per_ha = models.FloatField(blank=True, null=True)  # 每公顷蓄积（立方米）
    mean_tree_height = models.FloatField(blank=True, null=True)  # 平均树高（米）
    mean_dbh = models.FloatField(blank=True, null=True)  # 平均胸径（厘米）
    forest_stand_biomass = models.FloatField(blank=True, null=True)  # 林分生物量
    coefficient = models.FloatField(blank=True, null=True)  # 系数
    remark = models.TextField(blank=True, null=True)

    class Meta:
        db_table = 'resource'

    def __str__(self):
        return str(self.area)


class ResourceSerializer(serializers.ModelSerializer):
    province = serializers.StringRelatedField(source='address.province')
    city = serializers.StringRelatedField(source='address.city')
    county = serializers.StringRelatedField(source='address.county')
    town = serializers.StringRelatedField(source='address.town')
    village = serializers.StringRelatedField(source='address.village')

    vegetation_ecological_zone_name = serializers.StringRelatedField(source='vegetation_ecological_zone.name')
    ecological_engineering_zone_name = serializers.StringRelatedField(source='ecological_engineering_zone.name')
    ecological_zone_2 = serializers.SerializerMethodField()
    ecological_zone_name = serializers.StringRelatedField(source='ecological_zone.name')
    forest_class_name = serializers.StringRelatedField(source='forest_class.name')
    forest_class_level = serializers.StringRelatedField(source='forest_class.level')
    dominant_tree_species_name = serializers.StringRelatedField(source='dominant_tree_species.name')
    origin_name = serializers.StringRelatedField(source='origin.name')
    age_group_name = serializers.StringRelatedField(source='age_group.name')

    class Meta:
        model = Resource
        fields = '__all__'

    def get_ecological_zone_2(self, obj):
        return obj.ecological_zone.name + obj.forest_class.name
